import gcoord from "../../miniprogram_npm/gcoord/dist/gcoord";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shortName: '',
    corpCredit: '',
    agencyName: '',
    corpName: '',
    position: '',
    backNum: '',
    // 问卷的答案
    answers: {
      q1: '',
      q2: "",
      q3: "",
      q4: "",
      q5: "",
      q6: "",
      q7_1: "",
      q7_2: "",
      q7_3: "",
      q8_a: "",
      q8_b: "",
      q8_c: "",
      q8_d: "",
      q8_e: "",
      q8_f: "",
      q9_a: "",
      q9_b: "",
      q9_c: "",
      q9_d: "",
      q9_e: "",
      q9_f: "",
    },
    onlyView: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      shortName: options.shortName,
      corpCredit: options.corpCredit,
      corpName: options.corpName,
      backNum: options.backNum,
      answers: options.questionnaire ? JSON.parse(options.questionnaire) : this.data.answers
    })
  },
  onChange(e) {
    const key = e.currentTarget.dataset.key
    const item = e.currentTarget.dataset.item
    if (this.data.answers[key] === item) {
      this.data.answers[key] = ''
    } else {
      this.data.answers[key] = item
    }
    this.setData({
      answers: this.data.answers
    })
  },
  bindKeyInput(e) {
    const key = e.currentTarget.dataset.key
    const value = e.detail.value
    this.data.answers[key] = value.replace(/\s+/g, '')
    this.setData({
      answers: this.data.answers
    })
  },
  async goSub() {
    // const answers = Object.keys(this.data.answers)
    // const ansValues = Object.values(this.data.answers)
    // // 校验多选项
    // const q5List = ansValues.filter((val, index) => answers[index].indexOf('q5') > -1)
    // const q7List = ansValues.filter((val, index) => answers[index].indexOf('q7') > -1)
    // if (!q5List.some(val => val) || !q7List.some(val => val)) {
    //   wx.showToast({
    //     title: '请填写问卷后提交',
    //     icon: 'none'
    //   })
    //   return
    // }
    // 校验全填项
    // const isHas = answers.some(val => {
    //   if (val.indexOf('q5') > -1 || val.indexOf('q7') > -1) {
    //     return false
    //   } else {
    //     if (!this.data.answers[val]) {
    //       return true
    //     }
    //   }
    // })
    const answers = Object.values(this.data.answers)
    const isHas = answers.some(val => val)
    if (!isHas) {
      wx.showToast({
        title: '请填写问卷后提交',
        icon: 'none'
      })
      return
    }
    console.log(this.data.answers, 'this.data.answers');
    const userInfo = wx.getStorageSync('userInfo')
    const params = {
      userId: userInfo.userId,
      corpCredit: this.data.corpCredit,
      questionnaire: this.data.answers,
      questionnaireStatus: '问卷已完成'
    }
    console.log(params, 'params')
    const res = await getApp().globalData.request(
      "/visit/updateVisitCorpQuestionnaire", {
        ...params
      },
      "POST"
    );
    console.log(res, 'res')
    if (res.code == 200) {
      wx.navigateTo({
        url: `/pages/commitsucceed/commitsucceed?backNum=${this.data.backNum}&text=${"问卷已提交成功"}`,
      })
    } else {
      wx.showToast({
        title: '提交失败',
        icon: 'none'
      })
    }
  },
  onChangeMore(e) {
    const value = e.currentTarget.dataset.value
    const key = e.currentTarget.dataset.key
    if (this.data.answers[key]) {
      this.data.answers[key] = ''
    } else {
      this.data.answers[key] = value
    }
    this.setData({
      answers: this.data.answers
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});